package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbDict;
import com.bwie.service.TbDictService;
import com.bwie.mapper.TbDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 28242
 * @description 针对表【tb_dict(字典表)】的数据库操作Service实现
 * @createDate 2025-06-07 11:00:06
 */
@Service
public class TbDictServiceImpl extends ServiceImpl<TbDictMapper, TbDict>
        implements TbDictService {

    @Autowired
    StringRedisTemplate redisTemplate;


    @Autowired
    TbDictMapper tbDictMapper;

    @PostConstruct
    public void LoadDict() {
        //项目初始化时缓存字典

        System.out.println("加载字典缓存");

        List<TbDict> tbDicts = tbDictMapper.selectList(null);


        for (TbDict tbDict : tbDicts) {

            String key1 = "dict-" + tbDict.getDictType();
            String key2 = tbDict.getDictValue().toString();
            String value = JSON.toJSONString(tbDict);
            redisTemplate.opsForHash().put(key1, key2, value);

        }


    }


}




